﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BookStore.Solution.Entities;
using BookStore.Solution.BLL;
using BookStore.Solution.Utilities;

namespace BookStore.Solution.Presentation
{
    public partial class frmIE : DevComponents.DotNetBar.OfficeForm
    {
        public IE NewIE = null;
        private IEManager manager = new IEManager();
        private bool IsAdd;

        private void ResetForm()
        {
            txtEmployeeID.Text = AccountManager.CurrentAccount.Employee.EmployeeName;
            txtAmount.Text = DataUtil.ChangeMoneyToString(0);
            txtReason.Text = "";
            dtiIEDate.Value = DateTime.Now;
        }

        private void LoadEntity()
        {
            txtEmployeeID.Text = NewIE.Employee.EmployeeName;
            txtAmount.Text = DataUtil.ChangeMoneyToString(NewIE.Amount);
            txtReason.Text = NewIE.Reason;
            dtiIEDate.Value = NewIE.IEDate;
        }

        private IE GetEntity()
        {
            IE entity = new IE();
            entity.EmployeeID = AccountManager.CurrentAccount.Employee.EmployeeID;
            entity.IEDate = dtiIEDate.Value;
            entity.Reason = txtReason.Text;
            entity.Amount = DataUtil.ChangeStringToMoney(txtAmount.Text);
            entity.Status = "Khả dụng";
            if (!IsAdd)
            {
                entity.IEID = NewIE.IEID;
                entity.EmployeeID = NewIE.EmployeeID;
            }
            return entity;
        }

        private void ResetColor()
        {
            txtAmount.Border.BackColor =
            txtReason.Border.BackColor = StyleManager.ColorTint;

        }

        void AddEntity()
        {
            string result = CheckValidated();
            if (result == "")
            {
                NewIE = GetEntity();
                result = manager.Insert(NewIE);
                if (result == "Thêm thành công")
                {
                    MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Close();
                }
            }
            else
                MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        void UpdateEntity()
        {
            string result = CheckValidated();
            if (result == "")
            {
                NewIE = GetEntity();
                result = manager.Update(NewIE);
                if (result == "Cập nhật thành công")
                {
                    MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Close();
                }
            }
            else
                MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private string CheckValidated()
        {
            string result = "";
            if (DataUtil.ChangeStringToMoney(txtAmount.Text)==0)
            {
                result += "Nhập số tiền không đúng. ";
                txtAmount.Border.BackColor = Color.Red;
            }
            if (txtReason.Text == "")
            {
                result += "Lý do chi tiêu rỗng. ";
                txtReason.Border.BackColor = Color.Red;
            }
            return result;
        }

        public frmIE()
        {
            InitializeComponent();
            IsAdd = true;
        }

        public frmIE(IE entity)
        {
            InitializeComponent();
            IsAdd = false;
            NewIE = manager.GetSignle(entity.IEID);
            Text = "Cập nhật cho chi tiêu có mã: " + entity.IEID;
            btnSubmit.Text = "Cập nhật";
        }

        private void txtAmount_Leave(object sender, EventArgs e)
        {
            txtAmount.Text = DataUtil.ChangeMoneyToString(DataUtil.ToLong(txtAmount.Text));
        }

        private void txtAmount_Enter(object sender, EventArgs e)
        {
            txtAmount.Text = DataUtil.ChangeStringToMoney(txtAmount.Text).ToString();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (IsAdd)
            {
                AddEntity();
            }
            else
            {
                UpdateEntity();
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            ResetColor();
            ResetForm();
            if (!IsAdd)
                LoadEntity();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            NewIE = null;
            Close();
        }

        private void frmIE_Load(object sender, EventArgs e)
        {
            ResetForm();
            if (!IsAdd)
                LoadEntity();
        }
    }
}